#ifndef _DATA_DB_STRUCT_H_
#define _DATA_DB_STRUCT_H_
#include "data_flood.h"

typedef struct ItemIdData {
    unsigned lp_off : 15,
        lp_flags : 2,
        lp_len : 15;
} ItemIdData;

typedef ItemIdData* ItemId;

typedef struct HeapTupleFields {
    uint32 t_xmin;
    uint32 t_xmax;

    union {
        uint32 t_cid;           /* inserting or deleting command ID, or both */
        uint32 t_xvac; /* old-style VACUUM FULL xact ID */
    } t_field3;
} HeapTupleFields;

typedef struct DatumTupleFields {
    uint32 datum_len_;
    uint32 datum_typmod;
    uint32 datum_typeid;
} DatumTupleFields;

typedef struct BlockIdData {
    uint16 bi_hi;
    uint16 bi_lo;
} BlockIdData;

typedef struct ItemPointerData {
    BlockIdData ip_blkid;
    OffsetNumber ip_posid;
} ItemPointerData;

typedef struct {
    uint32 xlogid;  /* high bits */
    uint32 xrecoff; /* low bits */
} PageXLogRecPtr;

/* openGauss code */
typedef struct {
    PageXLogRecPtr pd_lsn;
    uint16 pd_checksum;
    uint16 pd_flags;
    uint16 pd_lower;
    uint16 pd_upper;
    uint16 pd_special;
    uint16 pd_pagesize_version;
    uint32 pd_prune_xid;
    uint64 pd_xid_base;                 /* base value for transaction IDs on page */
    uint64 pd_multi_base; 
    ItemIdData pd_linp[1];
} HeapPageHeaderData;
typedef HeapPageHeaderData* HeapPageHeader;
#define SizeOfHeapPageHeaderData (offsetof(HeapPageHeaderData, pd_linp))

typedef struct HeapTupleHeaderData {
    union {
        HeapTupleFields t_heap;
        DatumTupleFields t_datum;
    } t_choice;
    ItemPointerData t_ctid;
    uint16 t_infomask2;
    uint16 t_infomask;
    uint8 t_hoff;

    bits8 t_bits[1];
} HeapTupleHeaderData;

#define HeapPageGetItemId(page, offsetNumber) ((ItemId)(&((HeapPageHeader)(page))->pd_linp[(offsetNumber)-1]))
#define PageGetItem(page, itemId) ((char*)(((char*)(page)) + ItemIdGetOffset(itemId)))
#define ItemIdGetOffset(itemId) ((itemId)->lp_off)

#define CHECKSUM_COMP(checksum, value)                  \
    do {                                                \
        uint32 __tmp = (checksum) ^ (value);            \
        (checksum) = __tmp * FNV_PRIME ^ (__tmp >> 17); \
    } while (0)

#endif